package com.huaruan.youdi.views.activitys;

import android.Manifest;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import com.huaruan.youdi.R;
import com.huaruan.youdi.utils.AppUtils;
import com.huaruan.youdi.utils.PreferenceUtil;
import com.huaruan.youdi.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class WelcomeActivity extends BaseActivity {

    String[] permissions = new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.READ_PHONE_STATE};
    List<String> mPermissionList = new ArrayList<>();
    private final int mRequestCode = 100;

    @Override
    public int IGetContentViewResId() {
        return R.layout.activity_welcome;
    }

    @Override
    public void IFindViews() {
        initPhoto();
    }

    @Override
    public void IInitData() {

    }

    private void initPhoto() {
        final Runnable runnable = new Runnable() {
            @Override
            public void run() {
                initPermission();
            }
        };
        getHandler().postDelayed(runnable, 2000);
    }


    private void initPermission() {

        mPermissionList.clear();

        for (int i = 0; i < permissions.length; i++) {
            if (ContextCompat.checkSelfPermission(this, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
                mPermissionList.add(permissions[i]);
            }
        }
        if (mPermissionList.size() > 0) {
            ActivityCompat.requestPermissions(this, permissions, mRequestCode);
        } else {
            String uid = PreferenceUtil.getString(PreferenceUtil.PreferenceKeys.UID, "");
            if (StringUtils.isEmpty(uid)) {
                AppUtils.jump(IGetContext(), LoginActivity.class);
            } else {
                AppUtils.jump(IGetContext(), MainActivity.class);
            }
            finish();
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        boolean hasPermissionDismiss = false;//有权限没有通过
        if (mRequestCode == requestCode) {
            for (int i = 0; i < grantResults.length; i++) {
                if (grantResults[i] == -1) {
                    hasPermissionDismiss = true;
                }
            }
            //如果有权限没有被允许
            if (hasPermissionDismiss) {
                AppUtils.toastShort(IGetContext(), "请先允许应用所需权限");
                initPermission();
            } else {
                String token = PreferenceUtil.getString(PreferenceUtil.PreferenceKeys.UID, "");
                if (StringUtils.isEmpty(token)) {
                    AppUtils.jump(IGetContext(), LoginActivity.class);
                } else {
                    AppUtils.jump(IGetContext(), MainActivity.class);
                }
                finish();
            }
        }

    }


    @Override
    public boolean isFullScreenAndHideStatusBar() {
        return true;
    }




}
